﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;

namespace Apps.DAL
{
    public class DbContext<T> where T : class, new()
    {
        //注意：不能写成静态的
        public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作
        public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(Db); } }//用来操作当前表的数据


        public DbContext()
        {
            string connection = System.Configuration.ConfigurationManager.ConnectionStrings["OracleDbContext"].ConnectionString;
            Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = connection,//  "DATA SOURCE = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)))(CONNECT_DATA = (SERVER = orcl)(SERVICE_NAME = orcl)));PASSWORD = qwer1234;PERSIST SECURITY INFO = True;USER ID = C##FIS;",
                DbType = DbType.Oracle,
                InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
                IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了

            });
            //调式代码 用来打印SQL 
            Db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                Console.WriteLine();
                Console.WriteLine(sql);
                Console.WriteLine();
                //Console.WriteLine(Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                Console.WriteLine();
            };
            //SQL报错
            Db.Aop.OnError = (exp) =>
            {
                Console.WriteLine(exp.Sql);
                //exp.sql 这样可以拿到错误SQL            
            };
        }
    }

    public class SqlDbContext 
    {
        //注意：不能写成静态的
        public SqlSugarClient SQLDb;//用来处理事务多表查询和复杂的操作

        public SqlDbContext()
        {
            string connection = System.Configuration.ConfigurationManager.ConnectionStrings["ConnWlSystem"].ConnectionString;
            SQLDb = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = connection,//  "DATA SOURCE = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)))(CONNECT_DATA = (SERVER = orcl)(SERVICE_NAME = orcl)));PASSWORD = qwer1234;PERSIST SECURITY INFO = True;USER ID = C##FIS;",
                DbType = DbType.SqlServer,
                InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
                IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了

            });
            //调式代码 用来打印SQL 
            SQLDb.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                Console.WriteLine();
                Console.WriteLine(sql);
                Console.WriteLine();
                //Console.WriteLine(Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                Console.WriteLine();
            };
            //SQL报错
            SQLDb.Aop.OnError = (exp) =>
            {
                Console.WriteLine(exp.Sql);
                //exp.sql 这样可以拿到错误SQL            
            };
        }
    }
}
